<?php

class Musers extends TST_Model {

    protected $table_users = 'users';

    function Musers() {
        parent::TST_Model();
        $this->selectDefault();
    }

    public function selectDefault() {
        $this->selected_columns = "*";
        return $this;
    }

    public function IdentifyUser($username,$status = "active")
    {
        $sql = "SELECT {$this->selected_columns}
        FROM {$this->table_users}
        WHERE name = ?
        AND status = ?
        ";
        return $this->query_row($sql, array($username, $status));
    }
    
    public function Update_infor($user, $pass)
    {
        $data = array(
                    'password'  => $pass,
                    'status' => 'active'
                    );
        $where =    "name = '{$user}'                    
                    ";
        $sql = $this->db->update_string($this->table_users, $data, $where);
        return $this->db->query($sql);   
    }
    
    public function Update_Paypal($user, $new_paypal)
    {
        $data = array(
                    'acount_paypal'  => $new_paypal,
                    );
        $where =    "name = '{$user}'                    
                    ";
        $sql = $this->db->update_string($this->table_users, $data, $where);
        return $this->db->query($sql);   
    }

    // ------ Table Users ----------------------
    /**
     *  add New Employees to table users
     * $data: array()
     */
    public function addNewUser($data) {
        $this->db->insert($this->table_users, $data);
        return $this->db->insert_id();
    }

    /**
     * update User Information to table users
     * @param type $data
     * @param type $id
     * @param type $status
     * @return type 
     */
    public function updateUser($data, $id, $status = AppContants::ACTIVE_STATUS) {
        $where = "id = '$id'
                    AND status = '$status'
                    ";
        $sql = $this->db->update_string($this->table_users, $data, $where);
        return $this->db->query($sql);
    }

    public function getUserByEmail($email_address, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT *
                FROM {$this->table_users}
                WHERE username = ?
                AND status = ?
                ";
        return $this->query_row($sql, array($email_address, $status));
    }

    public function checkEmailExist($email, $status) {
        $sql = "SELECT id
                FROM {$this->table_users}
                WHERE username = ?
                AND status != ?
                ";
        $res = $this->query_row($sql, array($email, $status));
        if ($res != null) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function checkUserRight($id, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT {$this->table_groups}.name as groups_name
                        
                FROM users, {$this->table_groups_users},{$this->table_groups}
                WHERE users.id = ? 
                        AND {$this->table_groups_users}.users_id = users.id 
                        AND {$this->table_groups}.id = {$this->table_groups_users}.groups_id    
                        AND {$this->table_groups_users}.status = ?";
        return $this->query_array($sql, array($id, $status));
    }

    public function getUsersByUserIdActivecode($userID, $active_code) {
        $sql = "SELECT id, username
                FROM	{$this->table_users}
                WHERE	id = ?
                        AND key_activate = ?";
        return $this->query_row($sql, array($userID, $active_code));
    }

}
